<?php

/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_course_topic extends course_topic {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }

    /*
     * 	取出一条信息
     */

    public function do_detail($ct_id = '') {
        $detail = array();
        $ct_id = isset($_REQUEST['ct_id']) ? $_REQUEST['ct_id'] : '';
        $detail = $this->one_info('course_topic', 'ct_id', $ct_id);
        //DEBUG 调试输出
        include template('course/cate/detail');
    }
    /*
     *	取出一条信息
     */
    public function do_detail_one($cg_id='') {    
    	global $_G;
    	$detail = array();
    	$ct_id = isset($_REQUEST['ct_id']) ? $_REQUEST['ct_id'] : '';
    	$detail = $this->one_info('course_topic','ct_id',$ct_id);
    	$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
    	//DEBUG 调试输出
    	if($detail){
    		$return['code'] = '0';
    		$return['data']=$detail;
    		echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
    	}else{
    		$return['code'] = '1';
    		$return['data']=array();
    		echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
    	}
    }

    /*
     *	取出专题列表信息
     */
    public function do_detail_list() {
    	global $_G;
    	$detail = array();
    	$page = max(1, intval($_GET['page']));
    	$perpage = $limit = isset($_REQUEST['limit']) && !empty($_REQUEST['limit']) ? $_REQUEST['limit'] : '10';
    	$start=(($page-1) * $perpage);
    	$wheresql = "";
    	$wheresql .= " AND isdelete = 0";
    	$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
    
    	//专题名称
    	$cg_name= isset($_REQUEST['cg_name']) && !empty($_REQUEST['cg_name']) ? $_REQUEST['cg_name'] : '';
    	if($cg_name){
    		$wheresql .= " AND cg_name LIKE '%".$cg_name."%'";
    	}
    	$orderby = " ORDER BY ct_id DESC ";
    	//DEBUG 初始化请求请求获取一页列表数据的参数
    	$page_condition=array(
    			'page' => $page,//int 请求页面 页码
    			'limit' => $limit,//int 每页请求个数
    			'perpage' => $perpage,//int 每页显示个数
    			'wheresql' => $wheresql,//string //条件SQL语句
    			'orderby' => $orderby,//string 排序规则
    	);
    	$page_result = $this->detail_list('course_topic',$page_condition);
    	//DEBUG 调试输出
    	if($page_result){
    		$return['code'] = '0';
    		$return['data']=$page_result;
    		echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
    	}else{
    		$return['code'] = '1';
    		$return['data']=array();
    		echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
    	}
    
    }
    
    /*
     *  添加专题
     */

    public function do_add() {
    	global $_G;	
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$course_topic = isset($_REQUEST['course_topic']) && !empty($_REQUEST['course_topic']) ? $_REQUEST['course_topic']:'';		
				
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		foreach($course_topic AS $key => $value){
			if(is_array($value) && !empty($value)){
				$course_topic[$key] = implode(",", $value);
			}
		}
		$course_topic['create_dateline'] = TIMESTAMP;
		$course_topic['modify_dateline'] = TIMESTAMP;
		$course_topic['user_id'] = $_G['user_id'];
		
		$insert_data = $this->add('course_topic',$course_topic);	
	
		if($insert_data){										
			$return['code'] = '0';
			$return['data']=$insert_data;	
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
		
    }

    /*
     *  编辑专题
     */

    public function do_edit() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $request_data = isset($_REQUEST['course_topic']) && !empty($_REQUEST['course_topic']) ? $_REQUEST['course_topic'] : '';
        if ($request_data['id']) {
            if ($_G['gp_api']) {
                //DEBUG 外部参数和数据库字段名称不一致情况下 需映射赋值
                $data['cg_img_small'] = $request_data['img'];
                $data['ct_url'] = $request_data['url'];
                $data['cg_name'] = $request_data['title'];
                $data['cg_name_sub'] = $request_data['sub'];
                if($request_data['frontpage_sort']){
                    $data['frontpage_sort'] = $request_data['frontpage_sort'];   
                }
                if($request_data['so_id']){
                	$data['so_id'] = $request_data['so_id'];
                }
                $data['modify_dateline'] = TIMESTAMP;
                $where = array('ct_id' => $request_data['id']);
                $effect_row = $this->edit('course_topic', $data, $where);
                if ($effect_row) {
                    $return['code'] = '0';
                } else {
                    $return['code'] = '1';
                }
            }
        } else {
            $return['code'] = '1';
        }
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do'],'pc','msg_admin','admin');
    }

    /*
     *  后台编辑专题
     */
    public function do_edit_admin() {
    	global $_G;
    	$course_topic = isset($_REQUEST['course_topic']) && !empty($_REQUEST['course_topic']) ? $_REQUEST['course_topic']:'';
    	$ct_id = isset($_REQUEST['ct_id']) ? $_REQUEST['ct_id'] : '';
    	if($ct_id){
    		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
    		foreach($course_topic AS $key => $value){
    			if(is_array($value) && !empty($value)){
    				$course_topic[$key] = implode(",", $value);
    			}
    		}
    		$course_topic['modify_dateline'] = TIMESTAMP;
    		$where = array('ct_id'=>$ct_id);
    		$effect_row = $this->edit('course_topic',$course_topic,$where);
    
    		if($effect_row){
    			$return['code'] = '0';
    			$return['data']=$course_topic;
    			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
    		}else{
    			$return['code'] = '1';
    			$return['data']=array();
    			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
    		}
    	}else{
    		$return['code'] = '3';
    		$return['data']=array();
    		echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
    	}
    }
    /*
     *  删除专题
     */

    public function do_delete() {
    	global $_G;
        $ct_id = isset($_REQUEST['ct_id']) ? $_REQUEST['ct_id'] : '';      
        if($ct_id){
            $effect_row = $this->delete("course_topic", array("ct_id" => $ct_id));
            if($effect_row){
                $return['code'] = '0';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
            }else{           	
                $return['code'] = '1';
            	$return['data']=array();
            	echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
            }
        }else{
            $return['code'] = '3';
            $return['data']=array();
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
        global $_G;
        $api = isset($_REQUEST['api']) ? $_REQUEST['api'] : 'json';
        $client = isset($_REQUEST['client']) && !empty($_REQUEST['client']) ? $_REQUEST['client'] : 'wap';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 100000;
        $start = (($page - 1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if ($keyword) {
            $wheresql .= " AND cc_name LIKE '%" . $keyword . "%' ";
        }
        $cc_type = isset($_REQUEST['cc_type']) ? $_REQUEST['cc_type'] : '';
        if ($cc_type) {
            $wheresql .= " AND cc_type = '" . $cc_type . "' ";
        }
        $cc_level = isset($_REQUEST['cc_level']) ? $_REQUEST['cc_level'] : '';
        if ($cc_level) {
            //$wheresql .= " AND cc_level = '".$cc_level."' ";
        }
        $cc_parent_id = isset($_REQUEST['cc_parent_id']) ? $_REQUEST['cc_parent_id'] : "0";
        if ($cc_parent_id && $cc_level) {
            $wheresql .= " AND cc_parent_id = '" . $cc_parent_id . "' ";
        }
        $orderby = " ORDER BY ct_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->index('course_topic', $page_condition);
        //DEBUG 格式化输出
        if ($client == 'wap') {
            foreach ($page_result['page_data'] AS $key => $value) {
                $tmp_value['v'] = $value['ct_id'];
                $tmp_value['p'] = $value['cc_parent_id'];
                //$tmp_value['l'] = $value['cc_level'];
                $tmp_value['n'] = $value['cc_name'];
                $page_result['page_data'][$key] = $tmp_value;
            }
            //DEBUG 格式化树形数组
            $page_result['page_data'] = $this->arr2tree($page_result['page_data'], $cc_parent_id);
        } elseif ($client == 'pc') {
            foreach ($page_result['page_data'] AS $key => $value) {
                $tmp_value['key'] = $value['ct_id'];
                $tmp_value['p'] = $value['cc_parent_id'];
                //$tmp_value['l'] = $value['cc_level'];
                $tmp_value['label'] = $value['cc_name'];
                $page_result['page_data'][$key] = $tmp_value;
            }
            //print_r($page_result);
            //DEBUG 格式化树形数组
            $page_result['page_data'] = $this->arr2tree_pc($page_result['page_data'], $cc_parent_id);
            $root_node = array('key' => '0', 'label' => '全部');
            array_unshift($page_result['page_data'], $root_node);
            //$page_result['page_data'][]=$root_node;
        }
        if ($_G['gp_api']) {
            $return['code'] = '0';
            if ($client == 'wap') {
                $return['data'][$_G['gp_mod'] . '_' . $_G['gp_action'] . '_' . $_G['gp_do']] = $page_result;
            } else {
                $return['data'] = $page_result['page_data'];
            }
            $data = isset($_REQUEST['data']) ? $_REQUEST['data'] : "";
            if ($data) {
                echo json_ext($page_result['page_data']);
                die;
            } else {
                echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do'], $client);
            }
        } else {
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=course&action=cate&do=index&keyword=" . $keyword);
            include template('course/cate/index');
        }
    }

    /*
     * 后台信息分类数据数组树格式化函数
     * @pram 一维 array $array
     * @return 一维 $array tree
     */

    public function arr2tree($tree, $rootId = 0) {
        $return = array();
        foreach ($tree as $leaf) {
            if ($leaf['p'] == $rootId) {
                foreach ($tree as $subleaf) {
                    if ($subleaf['p'] == $leaf['v']) {
                        $leaf['s'] = $this->arr2tree($tree, $leaf['v']);
                        break;
                    }
                }
                $return[] = $leaf;
            }
        }
        return $return;
    }

    /*
     * 后台信息分类数据数组树格式化函数 FOR PC
     * @pram 一维 array $array
     * @return 一维 $array tree
     */

    public function arr2tree_pc($tree, $rootId = 0) {
        $return = array();
        foreach ($tree as $leaf) {
            if ($leaf['p'] == $rootId) {
                foreach ($tree as $subleaf) {
                    if ($subleaf['p'] == $leaf['key']) {
                        $leaf['children'] = $this->arr2tree_pc($tree, $leaf['key']);
                        break;
                    }
                }
                unset($leaf['p']);
                $return[] = $leaf;
            }
        }
        return $return;
    }

    /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
    public function do_api() {
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if ($keyword) {
            $wheresql = " WHERE title LIKE '%" . $keyword . "%'";
        }
        $orderby = " ORDER BY ct_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->api('course_topic', $page_condition);
        if ('json' == $return_type) {
            $return = json_encode($page_result);
        } elseif ('xml' == $return_type) {
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
        echo $return;
    }

}

?>